import * as Cesium from 'cesium/Cesium'
import PolylinePrimitive from "./PolylinePrimitive";
import PolylineDrawing from "./PolylineDrawing";
import DrawingMode from "./DrawingMode";

var clickDistanceScratch$1 = new  Cesium.Cartesian2
    , cart3Scratch$1 = new  Cesium.Cartesian3
    , cart3Scratch1$2 = new  Cesium.Cartesian3
    , normalScratch$7 = new  Cesium.Cartesian3
    , rayScratch$3 = new  Cesium.Ray
    , v1Scratch$1 = new  Cesium.Cartesian3
    , v2Scratch$1 = new  Cesium.Cartesian3
    , cartoScratch$3 = new  Cesium.Cartographic
    , mouseDelta$1 = 10;

function HorizontalPolylineDrawing(e) {
    e = Cesium.defaultValue(e, Cesium.defaultValue.EMPTY_OBJECT),
        PolylineDrawing.call(this, e);
    var t = Cesium.defaultValue(e.polylineOptions, Cesium.defaultValue.EMPTY_OBJECT)
        , i = {
        color: t.color,
        ellipsoid: t.ellipsoid,
        width: 2,
        dashed: !0
    }
        , r = this._primitives.add(new PolylinePrimitive(i));
    r.positions = [new Cesium.Cartesian3, new Cesium.Cartesian3],
        r.show = !1,
        this._dashLineOptions = i,
        this._dashedLines = [],
        this._moveDashLine = r,
        this._heightPlane = new Cesium.Plane(Cesium.Cartesian3.UNIT_X,0),
        this._heightPlaneCV = new Cesium.Plane(Cesium.Cartesian3.UNIT_X,0),
        this._firstMove = !1,
        this._height = 0
}

HorizontalPolylineDrawing.prototype = Object.create(PolylineDrawing.prototype),
    HorizontalPolylineDrawing.prototype.constructor = HorizontalPolylineDrawing,
    HorizontalPolylineDrawing.prototype._setDashLinePositions = function(e, t) {
        var i = this._scene.globe
            , r = this._scene.frameState.mapProjection.ellipsoid
            , n = e.positions;
        n[0] = Cesium.Cartesian3.clone(t, n[0]);
        var a = r.cartesianToCartographic(t, cartoScratch$3);
        Cesium.defined(i) ? a.height = Cesium.defaultValue(i.getHeight(a), 0) : a.height = 0,
            n[1] = r.cartographicToCartesian(a, n[1]),
            e.positions = n
    }
    ,
    HorizontalPolylineDrawing.prototype.addPoint = function(e) {
        PolylineDrawing.prototype.addPoint.call(this, e);
        var t = this._primitives.add(new PolylinePrimitive(this._dashLineOptions));
        t.positions = [new Cesium.Cartesian3, new Cesium.Cartesian3],
            this._dashedLines.push(t),
            this._setDashLinePositions(t, e)
    }
    ,
    HorizontalPolylineDrawing.prototype.handleDoubleClick = function() {
        this._mode = DrawingMode.AfterDraw,
            this._polyline.positions = this._positions,
            this._moveDashLine.show = !1
    }
    ,
    HorizontalPolylineDrawing.prototype.handleClick = function(e) {
        var t;
        if (0 === this._positions.length) {
            var i = this._scene
                , r = i.frameState.mapProjection.ellipsoid;
            if (!Cesium.defined(t = PolylineDrawing.prototype.handleClick.call(this, e)))
                return;
            this._heightPlane = Cesium.Plane.fromPointNormal(t, r.geodeticSurfaceNormal(t, normalScratch$7), this._heightPlane);
            var n = r.cartesianToCartographic(t, cartoScratch$3)
                , a = i.mapProjection.project(n, cart3Scratch1$2)
                , o = Cesium.Cartesian3.fromElements(a.z, a.x, a.y, a);
            this._heightPlaneCV = Cesium.Plane.fromPointNormal(o, Cesium.Cartesian3.UNIT_X, this._heightPlaneCV),
                this._height = r.cartesianToCartographic(t, cartoScratch$3).height,
                this._firstMove = !0
        } else {
            var s = this._lastClickPosition;
            if (Cesium.Cartesian2.magnitude(Cesium.Cartesian2.subtract(s, e, clickDistanceScratch$1)) < mouseDelta$1)
                return;
            Cesium.Cartesian2.clone(e, s),
                t = Cesium.Cartesian3.clone(this._tempNextPos),
                this.addPoint(t),
                this._firstMove = !0
        }
        return t
    }
    ,
    HorizontalPolylineDrawing.prototype.handleMouseMove = function(e, t) {
        if (this._mode === DrawingMode.Drawing) {
            var i, r = this._scene, n = r.frameState.mapProjection.ellipsoid,
                a = this._positions, o = r.camera.getPickRay(e, rayScratch$3);
            if (r.mode === Cesium.SceneMode.SCENE3D)
                i = Cesium.IntersectionTests.rayPlane(o, this._heightPlane, cart3Scratch$1);
            else if (r.mode === Cesium.SceneMode.COLUMBUS_VIEW) {
                i = Cesium.IntersectionTests.rayPlane(o, this._heightPlaneCV, cart3Scratch$1),
                    i = Cesium.Cartesian3.fromElements(i.y, i.z, i.x, i);
                var s = r.mapProjection.unproject(i, cartoScratch$3);
                i = n.cartographicToCartesian(s, i)
            } else if (Cesium.defined(i = r.camera.pickEllipsoid(e, n, cart3Scratch$1))) {
                var l = n.cartesianToCartographic(i, cartoScratch$3);
                l.height = this._height,
                    i = n.cartographicToCartesian(l, i)
            }
            if (Cesium.defined(i)) {
                if (!this._firstMove && t) {
                    var c = a[a.length - 1]
                        , u = this._tempNextPos
                        , d = Cesium.Cartesian3.subtract(u, c, v1Scratch$1)
                        , h = Cesium.Cartesian3.subtract(i, c, v2Scratch$1);
                    h =Cesium. Cartesian3.projectVector(h, d, h),
                        i = Cesium.Cartesian3.add(c, h, i)
                }
                return (a = a.slice()).push(Cesium.Cartesian3.clone(i, this._tempNextPos)),
                    this._polyline.positions = a,
                    this._firstMove = !1,
                    this._moveDashLine.show = !0,
                    this._setDashLinePositions(this._moveDashLine, i),
                    i
            }
        }
    }
    ,
    HorizontalPolylineDrawing.prototype.isDestroyed = function() {
        return !1
    }
    ,
    HorizontalPolylineDrawing.prototype.destroy = function() {
        for (var e = this._primitives, t = this._dashedLines, i = 0; i < t.length; i++)
            e.remove(t[i]);
        e.remove(this._moveDashLine),
            PolylineDrawing.prototype.destroy.call(this)
    }
export default HorizontalPolylineDrawing;